package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.BannerDTO;
import com.pshcp.domain.model.Banner;
import com.pshcp.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * banner表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-14
 */
@Service
public class BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    public PageList<Banner> pageList(BannerDTO bannerDTO){
        if(bannerDTO.getPageNum() != null && bannerDTO.getPageSize() != null){
            PageHelper.startPage(bannerDTO.getPageNum(), bannerDTO.getPageSize());
        }
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        if(bannerDTO.getStatus() != null){
            criteria.andEqualTo("status", bannerDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<Banner> list = bannerMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<Banner> list(BannerDTO bannerDTO){
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        if(bannerDTO.getStatus() != null){
            criteria.andEqualTo("status", bannerDTO.getStatus());
        }
        example.setOrderByClause(" sequence desc");
        return bannerMapper.selectByExample(example);
    }

    public List<Banner> select(Banner banner){
        return bannerMapper.select(banner);
    }

    public int insert(Banner banner){
        banner.setId(SystemUtil.getId());
        banner.setStatus(CommonStatusEnum.INIT.getCode());
        banner.setCreatedTime(new Date());
        return bannerMapper.insert(banner);
    }

    public int update(Banner banner){
        banner.setUpdateTime(new Date());
        return bannerMapper.updateByPrimaryKeySelective(banner);
    }

    public Banner findById(Long id){
        return bannerMapper.selectByPrimaryKey(id);
    }

    public List<Banner> findByIds(List<Long> ids){
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return bannerMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        Banner banner = new Banner();
        banner.setStatus(CommonStatusEnum.DEL.getCode());
        return bannerMapper.updateByExampleSelective(banner, example);
    }

}
